<?php
class Bizprofile_Models_Dao_Pdo_Mysql_MessageDetail extends Tomato_Model_Dao
	implements Bizprofile_Models_Interface_MessageDetail
{
	public function convert($entity)
	{
		return new Bizprofile_Models_MessageDetail($entity); 
	}
	
	public function getById($id) 
	{
		$row = $this->_conn
					->select()
					->from(array('m' => $this->_prefix . 'enterprise_message_detail'))
					->joinLeft(array('e' => $this->_prefix . 'enterprise_message'), 'e.id = m.messageid', 
							   array('title' => 'title', 
							   		 'message' => 'message',
							   		 'fromuser_id' => 'fromuser_id',  	
							   		 'fromuser_name' => 'fromuser_name',  	
							   		 'fromenterprise_id' => 'fromenterprise_id',  	
							   		 'fromenterprise_name' => 'fromenterprise_name',  	
							   		 'touserarray' => 'touserarray',  	
							   		 'toenterprisearray' => 'toenterprisearray',  	
							   		 'dateline_send' => 'dateline_send',  	
							   		 'dateline_read' => 'dateline_read',  	
							   ))
					->where('m.id = ?', $id)
					->limit(1)
					->query()
					->fetch();
    	return (null == $row) ? null : new Bizprofile_Models_MessageDetail($row);
	}
	
	public function add($detail) 
	{
		$this->_conn->insert($this->_prefix . 'enterprise_message_detail', 
							array(								
								'messageid' 			=> $detail->messageid,
								'user_id'   			=> $detail->user_id,
								'enterprise_id'			=> $detail->enterprise_id,
								'messageread'			=> $detail->messageread,
								'parentmessageid'		=> $detail->parentmessageid
							));
		return $this->_conn->lastInsertId($this->_prefix . 'enterprise_message_detail');
	}
	
	public function update($detail) 
	{
		$data = array(
			'messageread'			=> $detail->messageread
		);		 
		return $this->_conn->update($this->_prefix . 'enterprise_message_detail', 
									$data, 
									array(
										'id = ?' => $detail->id,
									));
	}
	
	public function delete($id) 
	{
		return $this->_conn->delete($this->_prefix . 'enterprise_message_detail',
									array(
										'id = ?' => $id,
									));
	}
	
	public function find($offset = null, $count = null, $exp = null)
	{
		$select = $this->_conn
						->select()
						->from(array('m' => $this->_prefix . 'enterprise_message_detail'))
						->joinLeft(array('e' => $this->_prefix . 'enterprise_message'), 'e.id = m.messageid', array(
							 'title' => 'title', 
							 'message' => 'message',
					   		 'fromuser_id' => 'fromuser_id',  	
					   		 'fromuser_name' => 'fromuser_name',  	
					   		 'fromenterprise_id' => 'fromenterprise_id',  	
					   		 'fromenterprise_name' => 'fromenterprise_name',  	
					   		 'touserarray' => 'touserarray',  	
					   		 'toenterprisearray' => 'toenterprisearray',  	
					   		 'dateline_send' => 'dateline_send',  	
					   		 'dateline_read' => 'dateline_read',
						))
						;
		if ($exp) {
			if (isset($exp['user_id'])) {
				$select->where('m.user_id = ?', $exp['user_id']);
			}
			if (isset($exp['enterprise_id'])) {
				$select->where('m.enterprise_id = ?', $exp['enterprise_id']);
			}
			if (isset($exp['messageread'])) {
				$select->where('m.messageread = ?', $exp['messageread']);
			}
			if (isset($exp['parentmessageid']) && $exp['parentmessageid'] == 0 ) {
				$select->where('m.parentmessageid = ?', $exp['parentmessageid']);
			}			
			if (isset($exp['parentmessageid']) && $exp['parentmessageid'] != 0 ) {
				$select->where('m.parentmessageid != ?', 0);
			}			
		}
		$select->order('e.dateline_send DESC');
		
		if (is_int($offset) && is_int($count)) {
			$select->limit($count, $offset);
		}
		$rs = $select->query()->fetchAll();
		return new Tomato_Model_RecordSet($rs, $this);
	}
	
	public function count($exp)
	{
		$select = $this->_conn
						->select()
						->from(array('m' => $this->_prefix . 'enterprise_message_detail'), array('num_messages' => 'COUNT(*)'));
		if ($exp) {
			if (isset($exp['user_id'])) {
				$select->where('m.user_id = ?', $exp['user_id']);
			}
			if (isset($exp['enterprise_id'])) {
				$select->where('m.enterprise_id = ?', $exp['enterprise_id']);
			}
			if (isset($exp['messageread'])) {
				$select->where('m.messageread = ?', $exp['messageread']);
			}
			if (isset($exp['parentmessageid']) && $exp['parentmessageid'] == 0 ) {
				$select->where('m.parentmessageid = ?', $exp['parentmessageid']);
			}			
			if (isset($exp['parentmessageid']) && $exp['parentmessageid'] != 0 ) {
				$select->where('m.parentmessageid != ?', 0);
			}			
		}
		return $select->limit(1)->query()->fetch()->num_messages;
	}
}
